package com.zzccyy.jpdemo.fragment

import android.content.Intent
import android.view.View
import android.widget.TextView
import com.sztle.admodule.apiprocessor.OutRewardAdApi
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BaseLazyFragment
import com.zzccyy.jpdemo.dialog.BtDateDialog
import com.zzccyy.jpdemo.page.GfActivity
import com.zzccyy.jpdemo.page.JpCityActivity
import com.zzccyy.jpdemo.page.ResultActivity
import com.zzccyy.jpdemo.page.XBActivity
import com.zzccyy.jpdemo.page.YJActivity
import com.zzccyy.jpdemo.page.ZJActivity
import java.text.SimpleDateFormat
import java.util.Date

class Fragment1 : BaseLazyFragment(), View.OnClickListener {
    private var ttSearch: TextView? = null
    private var tDay: TextView? = null
    private var tAd1: TextView? = null
    private var tAd2: TextView? = null
    private var tDay1: TextView? = null
    private var llDay: View? = null
    private var ivfr22: View? = null
    private var llA1: View? = null
    private var llA2: View? = null
    private var r1: View? = null
    private var r2: View? = null
    private var r3: View? = null
    private var r4: View? = null
    private var btDateDialog: BtDateDialog? = null
    private var isType = false
    private var start: String? = "北京"
    private var end: String? = "上海"

    override fun setLayoutId(): Int {
        return R.layout.fr1
    }

    override fun init(view: View?) {
        ttSearch = view!!.findViewById(R.id.ttSearch)
        tDay = view.findViewById(R.id.tDay)
        tDay1 = view.findViewById(R.id.tDay1)
        tAd1 = view.findViewById(R.id.tAd1)
        tAd2 = view.findViewById(R.id.tAd2)
        llDay = view.findViewById(R.id.llDay)
        ivfr22 = view.findViewById(R.id.ivfr22)
        llA1 = view.findViewById(R.id.llA1)
        llA2 = view.findViewById(R.id.llA2)
        r1 = view.findViewById(R.id.r1)
        r2 = view.findViewById(R.id.r2)
        r3 = view.findViewById(R.id.r3)
        r4 = view.findViewById(R.id.r4)
        tAd1!!.setText(start)
        tAd2!!.setText(end)
        val sdf = SimpleDateFormat("yyyy年MM月dd日")
        val formattedDate = sdf.format(Date())
        tDay!!.setText(formattedDate)
    }

    override fun loadData() {
        ttSearch!!.setOnClickListener(this)
        llDay!!.setOnClickListener(this)
        ivfr22!!.setOnClickListener(this)
        llA1!!.setOnClickListener(this)
        llA2!!.setOnClickListener(this)
        r1!!.setOnClickListener(this)
        r2!!.setOnClickListener(this)
        r3!!.setOnClickListener(this)
        r4!!.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.r1 -> {
                //行李规范

                val intent = Intent(activity, GfActivity::class.java)
                startActivity(intent)
            }

            R.id.r2 -> {
                //行李规范
                val intent = Intent(activity, XBActivity::class.java)
                startActivity(intent)
            }

            R.id.r3 -> {
                //行李规范
                val intent = Intent(activity, YJActivity::class.java)
                startActivity(intent)
            }

            R.id.r4 -> {
                //行李规范
                val intent = Intent(activity, ZJActivity::class.java)
                startActivity(intent)
            }

            R.id.llA1 -> {
                val intent = Intent(activity, JpCityActivity::class.java)
                intent.putExtra("from", 1)
                startActivityForResult(intent, 1111)
            }

            R.id.llA2 -> {
                val intent = Intent(activity, JpCityActivity::class.java)
                intent.putExtra("from", 1)
                startActivityForResult(intent, 2222)
            }

            R.id.ivfr22 -> {
                if (!isType) {
                    tAd1!!.text = end
                    tAd2!!.text = start
                } else {
                    tAd1!!.text = start
                    tAd2!!.text = end
                }
                isType = !isType
            }

            R.id.llDay -> {
                if (btDateDialog == null) {
                    btDateDialog =
                        BtDateDialog(activity!!, object : BtDateDialog.OnViewClickListener {
                            override fun setData(data: String?) {
                                tDay!!.text = data
                                tDay1!!.visibility =
                                    if (isToday(data)) View.VISIBLE else View.INVISIBLE
                            }
                        })
                }
                btDateDialog!!.show()
            }

            R.id.ttSearch -> {
                OutRewardAdApi.showRewardAd(activity, "query_air") {
                    val intent = Intent(activity, ResultActivity::class.java)
                    intent.putExtra("date", tDay!!.text.toString())
                    intent.putExtra("start", tAd1!!.text.toString())
                    intent.putExtra("end", tAd2!!.text.toString())
                    intent.putExtra("from", 1)
                    startActivity(intent)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1111 && resultCode == 1000) {
            start = data!!.getStringExtra("city")
            tAd1!!.text = start
        } else if (requestCode == 2222 && resultCode == 1000) {
            end = data!!.getStringExtra("city")
            tAd2!!.text = end
        }
    }
}